package com.ibuu.demos.controller;

import com.ibuu.demos.pojo.Categories;
import com.ibuu.demos.pojo.News;
import com.ibuu.demos.pojo.Num;
import com.ibuu.demos.pojo.ShopingCar;
import com.ibuu.demos.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoriesController {
    @Autowired
    private CategoriesService categoriesService;


    @GetMapping("/list")
    public List<Categories> list(HttpServletRequest request) {
        List<Categories> categories = categoriesService.getCategories();
        String username = (String) request.getSession().getAttribute("username");

        for (Categories category : categories) {
            ShopingCar shopingCarById = categoriesService.getShopingCarById(category.getShopid(), username);
            if (shopingCarById != null) {
                category.setNum(shopingCarById.getNum());
            } else {
                category.setNum(0);
            }
        }

        return categories;
    }

    @GetMapping("/{kind}")
    public List<Categories> getCategoriesById(@PathVariable Integer kind) {
        List<Categories> categories = categoriesService.getCategoriesBykind(kind);
        return categories;
    }

    //购物车添加
@PostMapping("/add") // 购物车添加
public void addCategories(@RequestBody Categories categories, HttpServletRequest request) {
        categoriesService.addCategories(categories, request);
}
    //购物车减少
    @PostMapping("/jian")
    public void jianCategories(HttpServletRequest request,@RequestBody Categories categories) {

        categoriesService.jianCategories(categories,request);
        System.out.println("jian");

    }

    @GetMapping("/getShopingCar")
    public List<ShopingCar> getShopingCar(HttpServletRequest request) {
        String username = (String) request.getSession().getAttribute("username");
        List<ShopingCar> shopingCars = categoriesService.getShopingCar(username);
        return shopingCars;
    }
    //购物车按id查询
    @GetMapping("/getShopingCarById")
    public ShopingCar getShopingCarById(HttpServletRequest request,Integer id) {
        String username = (String) request.getSession().getAttribute("username");
        ShopingCar shopingCar = categoriesService.getShopingCarById(id,username);
        return shopingCar;
    }

}
